import { menuToRoutes, eachTreeData } from 'ele-admin';
import { HOME_PATH, LAYOUT_PATH, REDIRECT_PATH } from '@/config/setting';
import EleLayout from '@/layout/index.vue';
import RedirectLayout from '@/components/RedirectLayout';
import { iframeMixins } from '@/utils/iframe-mixin';

/**
 * 静态路由
 */
export const routes = [
  {
    path: '/login',
    component: () => import('@/views/login'),
    meta: { title: '登录' }
  },
  {
    path: '/forget',
    component: () => import('@/views/forget'),
    meta: { title: '忘记密码' }
  },
  // 404
  {
    path: '/404',
    component: () => import('@/views/exception/404')
  },
  // 403
  {
    path: '/403',
    component: () => import('@/views/exception/403')
  },
  // 如果有首页权限 ，则添加到页面，
  // {
  //   path: '/Home',
  //   component: EleLayout,  // 这里要使用EleLayout
  //   meta: { title: '首页' },
  //   children:
  //   [
  //     {
  //       path: '/home',
  //       component: () => import('@/views/home'),
  //       meta: { title: '首页' }
  //   },]
  // },
];

/**
 * 添加动态路由
 * @param menus 菜单数据
 * @param homePath 菜单数据的第一个页面地址
 */
export function getMenuRoutes(menus, homePath) {
  const routes = [
    // 用于刷新的路由
    {
      path: REDIRECT_PATH + '/:path(.*)',
      component: RedirectLayout,
      meta: { hideFooter: true }
    }
  ];



 let mr = menuToRoutes(menus, getComponent, [], undefined, undefined, iframeMixins)

  // 路由铺平处理
  eachTreeData(mr,
    (route) => {
      routes.push({ ...route, children: undefined });
    }
  );
  // 根据权限添加首页
  routes.map((m)=>{
    if(m.path =='/home'){
      m.component = () => import('@/views/home'); // 这里要使用EleLayout
      m.meta = { title: '首页' };
    }
  });

  return {
    path: LAYOUT_PATH,
    component: EleLayout,
    redirect: HOME_PATH ?? homePath,
    children: routes
  };
}

/**
 * 获取路由组件
 * @param component 组件名称
 */
function getComponent(component) {
  if (component) {
    //  return (resolve) => import('@/views/' + component);
     return (resolve) =>require([`@/views/${component}/index`], resolve);
  }


}
